package fun.tan90.net.socketd;

import lombok.extern.slf4j.Slf4j;
import org.noear.socketd.SocketD;
import org.noear.socketd.transport.core.Entity;
import org.noear.socketd.transport.core.Listener;
import org.noear.socketd.transport.core.Message;
import org.noear.socketd.transport.core.Session;

import java.io.IOException;

@Slf4j
public class SocketdServer {

    public static void main(String[] args) throws Exception {
        //::启动服务端
        SocketD.createServer("sd:tcp-java-nio")
                .config(c -> c.port(8602))
                .listen(new Listener() {
                    @Override
                    public void onOpen(Session session) throws IOException {
                        log.info("onOpen");
                    }

                    @Override
                    public void onMessage(Session session, Message message) throws IOException {
                        Entity entity = message.entity();
                        String data = entity.dataAsString();
                        log.info("onMessage {}", data);
                    }

                    @Override
                    public void onClose(Session session) {
                        log.info("onClose");
                    }

                    @Override
                    public void onError(Session session, Throwable error) {
                        log.info("onError");
                    }
                })
                .start();
        log.info("SocketdServer start");
        System.out.println("SocketdServer start");
    }
}
